﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form4 : Form
    {
        public void hide()
        {
            groupBox1.Visible = false;
            groupBox2.Visible = false;
            groupBox3.Visible = false;
            groupBox4.Visible = false;
            groupBox5.Visible = false;
            groupBox6.Visible = false;
            groupBox7.Visible = false;
            groupBox8.Visible = false;
        }
        private void Form4_Load(object sender, EventArgs e)
        {
            hide();
        }

        public Form4()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            view = "select *from [user];";
            string userid = textBox1.Text;
            string username = textBox2.Text;
            string pass = textBox3.Text;
            using(SqlConnection connection=new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                string query = "insert into [user](userid,username,[password],level)values(@userid,@username,@password,'1');";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    try
                    {
                        command.Parameters.AddWithValue("@userid", userid);
                        command.Parameters.AddWithValue("@username", username);
                        command.Parameters.AddWithValue("@password", pass);
                        command.ExecuteNonQuery();

                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);
                    this.dataGridView1.DataSource = dataTable;
                }
            }
        }

        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox1.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string userid = textBox4.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                string query = "delete from [user] where userid=@userid";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@userid", userid);
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                view = "select *from [user];";

                SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.dataGridView1.DataSource = dataTable;

            }
        }

        private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox2.Visible = true;
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox3.Visible = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string userid = textBox5.Text;
            string pass = textBox6.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                string query = "update [user] set password='@password' where userid='@userid'";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@password", pass);
                    command.Parameters.AddWithValue("@userid", userid);
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                view = "select *from [user];";

                SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.dataGridView1.DataSource = dataTable;
            }

        }
        private void 添加课程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox4.Visible = true;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            string classroomid = textBox7.Text;
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            DateTime dateTime = dateTimePicker1.Value;
            string section = comboBox2.Text;
            
            string coursename = textBox11.Text;
            string courseid= textBox12.Text;
            string teacherid = textBox13.Text;
            string deptid = textBox14.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                var query = "insert into courseschedule(classroom_id,course_date,course_section,course_id,teacher_id)values(@classroomid,@date,@section,@course_id,@teacher_id);";
                try
                {
                    SqlCommand command = new SqlCommand(query, connection);
                    command.Parameters.AddWithValue("@classroomid", classroomid);
                    command.Parameters.AddWithValue("@date", dateTime);
                    command.Parameters.AddWithValue("@section", section);
                    command.Parameters.AddWithValue("@course_id", courseid);
                    command.Parameters.AddWithValue("@teacher_id", teacherid);
                    command.ExecuteNonQuery();
                }catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
                var query1 = "insert into course(id,course_name,deptid)values(@course_id,@coursename,@deptid);";

                try
                {
                    SqlCommand command1 = new SqlCommand(query1, connection);
                    command1.Parameters.AddWithValue("@course_id", courseid);
                    command1.Parameters.AddWithValue("@coursename", coursename);
                    command1.Parameters.AddWithValue("@deptid", deptid);
                    command1.ExecuteNonQuery();

                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                view = "select *from courseschedule;";

                SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.dataGridView1.DataSource = dataTable;

            }

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void groupBox5_Enter(object sender, EventArgs e)
        {

        }

        private void 修改课程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox5.Visible = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string classroom_id = textBox22.Text;
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "yyyy-MM-dd";
            DateTime dateTime = dateTimePicker1.Value;
            string section = comboBox2.Text;
            string course_id = textBox18.Text;
            string coursename = textBox19.Text;
            string deptid = textBox17.Text;
            string teacher_id = textBox16.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                var query = "update course set id=@id,course_name=@coursename,deptid=@deptid where course.id=@id;";
                try
                {
                    SqlCommand command = new SqlCommand(query, connection);
                    command.Parameters.AddWithValue("@id", course_id);
                    command.Parameters.AddWithValue("@coursename", coursename);
                    command.Parameters.AddWithValue("@deptid", deptid);
                    command.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                var query1 = "update  courseschedule set classroom_id=@classroom_id,course_date=@date,course_section=@section,course_id=@course_id,teacher_id=@teacherid where course_id=@course_id;";
                try
                {
                    SqlCommand command = new SqlCommand(query1, connection);
                    command.Parameters.AddWithValue("@classroom_id", classroom_id);
                    command.Parameters.AddWithValue("@date", dateTime);
                    command.Parameters.AddWithValue("@section", section);
                    command.Parameters.AddWithValue("@course_id", course_id);
                    command.Parameters.AddWithValue("@teacherid",teacher_id);
                    command.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                view = "select *from courseschedule;";

                SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.dataGridView1.DataSource = dataTable;
            }
        }

        

        private void button7_Click(object sender, EventArgs e)
        {
            string classroom_id = textBox30.Text;
            dateTimePicker3.Format = DateTimePickerFormat.Custom;
            dateTimePicker3.CustomFormat = "yyyy-MM-dd";
            DateTime dateTime = dateTimePicker1.Value;
            string section = comboBox3.Text;
            string course_id = textBox26.Text;
            string coursename = textBox27.Text;
            string deptid = textBox25.Text;
            string teacher_id = textBox24.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                
                var query1 = "update courseschedule set [used]=null where course_id=@course_id;";
                try
                {
                    SqlCommand command = new SqlCommand(query1, connection);
                    
                    command.Parameters.AddWithValue("@course_id", course_id);
                    
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                view = "select *from courseschedule;";

                SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);
                this.dataGridView1.DataSource = dataTable;
            }
        }

        private void 删除课程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox6.Visible = true;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string id = textBox44.Text;
            string result = comboBox1.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                var query = "update borrow set result=@result where id=@id;";
                using(SqlCommand command=new SqlCommand (query,connection))
                {
                    command.Parameters.AddWithValue("@result", result);
                    command.Parameters.AddWithValue("@id", id);
                    command.ExecuteNonQuery();
                    
                }
                view = "select *from [borrow];";

                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);
                    this.dataGridView1.DataSource = dataTable;
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }

        private void 教师信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox7.Visible = true;
            view = "select *from teacher;";
            
            
            
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string teacher_id = textBox38.Text;
            string teacher_name = textBox37.Text;
            string sex = textBox36.Text;
            string tdeptid = textBox35.Text;
            string title = textBox34.Text;
            using (SqlConnection connection = new SqlConnection(SqlHelper.Constr))
            {
                connection.Open();
                var query = "insert into teacher(id,name,deptid,title,sex)values(@id,@name,@dept,@title,@sex)";
                try
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@id", teacher_id);
                        command.Parameters.AddWithValue("@name", teacher_name);
                        command.Parameters.AddWithValue("@dept", tdeptid);
                        command.Parameters.AddWithValue("@title", title);
                        command.Parameters.AddWithValue("@sex", sex);
                        command.ExecuteNonQuery();

                    }
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                try
                {
                    view = "select *from teacher;";

                    SqlDataAdapter adapter = new SqlDataAdapter(view, connection);

                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);
                    this.dataGridView1.DataSource = dataTable;
                    connection.Close();
                }catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }

        private void 申请信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox8.Visible = true;
            

        }
            string view;
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void 用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

        }

        private void 课程管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void groupBox6_Enter(object sender, EventArgs e)
        {

        }

        
    }
}
